package com.qianfeng.plateform2203.zuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.qianfeng.plateform2203.common.constants.RedisConstants;
import com.qianfeng.plateform2203.common.utils.Md5Util;
import com.qianfeng.plateform2203.zuul.feign.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@Component
public class SignFilter extends ZuulFilter {

    @Autowired
    CacheService cacheService;

    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 10;
    }

    @Override
    public boolean shouldFilter() {
        // 参考前面的过滤器的 判断结果，如果前面的过滤器已经认为请求有问题，会把currentContext.setSendZuulResponse(false);
        // 这样，当前过滤器就没有必要继续执行run方法了
        final RequestContext currentContext = RequestContext.getCurrentContext();
        final boolean b = currentContext.sendZuulResponse();
        return b;
    }

    @Override
    public Object run() throws ZuulException {

        // 取出请求中的所有参数 ，把除了sign之外的所有参数进行md5运算得到一个sign值
        final RequestContext currentContext = RequestContext.getCurrentContext();
        final HttpServletRequest request = currentContext.getRequest();

        final Enumeration<String> parameterNames = request.getParameterNames();

        final HashMap<String, String> paramMap = new HashMap<>();

        while (parameterNames.hasMoreElements()) {

            final String paramName = parameterNames.nextElement();
            if ("sign".equals(paramName)){
                continue;
            }
            final String paramValue = request.getParameter(paramName);
            paramMap.put(paramName,paramValue);

        }

        // secret参数，每个客户拥有自己的一个
        // 要求，请求的必传参数中，要传入 app_key 例如 ：海尔集团的 appkey 561AC1A8676CFCB0CC61B041AE42ABB8

        // 根据 app_key 查找出 561AC1A8676CFCB0CC61B041AE42ABB8
        final String app_key = request.getParameter("app_key");
        String key = RedisConstants.APP_INFO_PRE+app_key;
        try {
            final Map<Object, Object> appInfoMap = cacheService.hGetAll(key);
            final String secret = (String) appInfoMap.get("appSecret");
            final String serverMd5 = Md5Util.md5Signature(paramMap, secret);
            System.out.println("服务器端计算的md5："+serverMd5);
            final String sign = request.getParameter("sign");
            System.out.println("请求中传递的md5："+sign);

            if (serverMd5.equals(sign)){
                return null;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        // 请求有问题，不允许请求继续发给后面的微服务了
        currentContext.setSendZuulResponse(false);

        final HttpServletResponse response = currentContext.getResponse();
        response.setContentType("text/html;charset=utf-8");
        try {
            response.getWriter().println("签名有问题");
        } catch (IOException e) {
            e.printStackTrace();
        }



        return null;
    }
}
